package Sort.面试.信必优;

import java.util.List;

/**
 * 三个有序数组的合并
 */
public class MergeThreeArray {

    public static int[] MergeThreeNumArray(int[] a, int[] b, int[] c){

        int temp[] = MergeArray(a,b);

        int res[] = MergeArray(temp,c);

        return res;
    }


    public static int[] MergeArray(int[] arr1, int[] arr2){
        int len1 = arr1.length;
        int len2 = arr2.length;
        int i = 0;
        int j = 0;

        int[] res = new int[len1 + len2];
        int index = 0;

        while(i < arr1.length && j < arr2.length){
            if(arr1[i] <= arr2[j]){
                res[index] = arr1[i];
                i++;
            }else{
                res[index] = arr2[j];
                j++;
            }
            index ++;
        }

        if(i < arr1.length){
            for(int x = i; x < len1; x++){
                res[index++] = arr1[x];
            }
        }

        if(j < arr2.length){
            for(int x = j; x < len2; x++){
                res[index++] = arr2[x];
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] a = { 1, 2, 3, 4 };
        int[] b = { 6, 7, 9 };
        int[] c = { 5,8,10, 11, 12 };

        int res[] = MergeThreeNumArray(a,b,c);

        for (int i = 0; i < res.length; i++) {
            System.out.println(res[i]);
        }
    }
}
